package org.example.springbootwebexample.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.springbootwebexample.aop.Log;
import org.example.springbootwebexample.pojo.Emp;
import org.example.springbootwebexample.pojo.PageBean;
import org.example.springbootwebexample.pojo.Result;
import org.example.springbootwebexample.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;



    //模糊分页查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        PageBean pageBean =  empService.page(page, pageSize,name,gender,begin,end);

        return Result.success(pageBean);
    }


    //根据id 批量删除员工
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除员工 {}",ids);
        empService.delete(ids);

        return Result.success();
    }


    //新增员工
    @Log
    @PostMapping
    public Result insert(@RequestBody Emp emp){
        log.info("新增员工 {}",emp);

        empService.insert(emp);

        return Result.success();
    }


    //根据id 查询员工信息
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据id {} 查询用户", id);

        Emp emp = empService.selectById(id);

        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result updata(@RequestBody Emp emp){
        log.info("修改id为 {} 的员工数据" ,emp.getId());

        empService.update(emp);

        return Result.success();
    }


}
